<script>
  var sortArrayByParity = function(nums) {
    if (nums.length === 0 || nums.length === 1) {
      return nums
    }
    let j = nums.length - 1;
    for(let i = 0; i < nums.length; i++) {
      if(nums[i] % 2 === 1) {
        if(nums[j] % 2 === 0) {
          [nums[i], nums[j]] = [nums[j], nums[i]]
        } else {
          j--;
        }
      }
    }
  };

  console.log(sortArrayByParity([3, 1, 2, 4]))
  console.log(sortArrayByParity([0]));
</script>